Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PWA option to ensure cross-origin isolation headers on web export #86089

Merged
merged 1 commit into from
Feb 12, 2024

Conversation

adamscott
Copy link
Member

Sister PR to #85939

image

When the PWA option is enabled, the installed PWA will make sure to enable COEP/COOP request headers if the option is selected.

@adamscott adamscott requested review from a team as code owners December 12, 2023 20:53
@adamscott adamscott changed the title Add option to ensure cross-origin isolation headers on web export Add PWA option to ensure cross-origin isolation headers on web export Dec 12, 2023
@adamscott adamscott added this to the 4.x milestone Dec 12, 2023
@adamscott adamscott force-pushed the pwa-coop-coep branch 3 times, most recently from cd0c35e to ba6cb81 Compare December 12, 2023 21:16
@adamscott adamscott marked this pull request as draft December 14, 2023 15:52
@adamscott

This comment was marked as outdated.

@adamscott adamscott marked this pull request as ready for review December 14, 2023 16:52
@adamscott adamscott requested a review from Faless December 14, 2023 16:53
@adamscott adamscott force-pushed the pwa-coop-coep branch 2 times, most recently from 830601c to ad8b31d Compare December 14, 2023 18:05
Copy link
Collaborator

@Faless Faless left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this 💙 .
It's looking good overall 🚀 (see my comment on moving some code into engine.js).
I like the s/@/__ rename, I wish it was done in its own PR though (EDIT: We can probably refactor this further into something similar to GODOT_CONFIG in a separate PR).
There are some "style" changes (e.g. using the loose equality operator) which are not great in JS .
I'll look into normalizing the service worker using eslint like we do for other bits of the javascript.

Comment on lines +218 to +243
if (GODOT_CONFIG['serviceWorker'] && GODOT_CONFIG['ensureCrossOriginIsolationHeaders'] && 'serviceWorker' in navigator) {
// There's a chance that installing the service worker would fix the issue
Promise.race([
navigator.serviceWorker.getRegistration().then((registration) => {
if (registration != null) {
return Promise.reject(new Error('Service worker already exists.'));
}
return registration;
}).then(() => engine.installServiceWorker()),
// For some reason, `getRegistration()` can stall
new Promise((resolve) => {
setTimeout(() => resolve(), 2000);
}),
]).catch((err) => {
console.error('Error while registering service worker:', err);
}).then(() => {
window.location.reload();
});
} else {
// Display the message as usual
const missingMsg = 'Error\nThe following features required to run Godot projects on the Web are missing:\n';
displayFailureNotice(missingMsg + missing.join('\n'));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should move this to a function of platform/js/engine/engine.js like we did for Engine.getMissingFeatures.

This comment was marked as outdated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep the code as is, finally. We would need to pass in parameters to that function the config and the engine variable, which seems a little bit excessive.

@Faless
Copy link
Collaborator

Faless commented Dec 15, 2023

Attached the 2 patches (001 adds the service worker linting and its config, 002 is just a linter pass).
patch-001.diff.txt
patch-002.diff.txt

EDIT: Remember to git add platform/web/.eslintrc.sw.js

@adamscott
Copy link
Member Author

adamscott commented Dec 15, 2023

There are some "style" changes (e.g. using the loose equality operator) which are not great in JS .

Actually, this was done precisely with that in mind. null is pretty much the exception when using the loose equality operator. (see https://eslint.org/docs/latest/rules/eqeqeq#smart)

You actually want to compare a variable loosely to null. That's because Javascript considers null and undefined nullish (hence both values pass the loose equality test with null), but not the falsy values (eg. "", 0, which do not pass the loose equality test with null).

Hence why, instead, we should replace all the if (value) tests by value != null and if (!value) tests by value == null. (but that's for another PR)

undefined == null; // `true`
"" == null; // `false`

@adamscott
Copy link
Member Author

Attached the 2 patches (001 adds the service worker linting and its config, 002 is just a linter pass). patch-001.diff.txt patch-002.diff.txt

EDIT: Remember to git add platform/web/.eslintrc.sw.js

Done!

platform/web/package.json Outdated Show resolved Hide resolved
platform/web/package.json Outdated Show resolved Hide resolved
Copy link
Collaborator

@Faless Faless left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a rebase (tested here: https://github.com/Faless/godot/tree/temp/86089) and squash, then I think this is good to go.
Great job 👍

@akien-mga akien-mga modified the milestones: 4.x, 4.3 Feb 6, 2024
@adamscott adamscott force-pushed the pwa-coop-coep branch 2 times, most recently from 95a68e5 to b484cd5 Compare February 12, 2024 14:27
@adamscott
Copy link
Member Author

Rebase done!

@akien-mga akien-mga merged commit 1b55fa1 into godotengine:master Feb 12, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks! Great feature to have :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants